
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Text;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Container;
 
namespace Contacts
{
	public class ContactsBizRules : Syrinx.BaseBizRules
	{
		public void addContactInquiry(IApplication app, IRuleContext context, IBizObj contact)
		{
			try
			{
				if (contact == null)
					throw new ContactsException("error executing addContactInquiry. Contact is null.");

				context.put("email",ActiveCompany.get("settings.defaultCompanyEmail") );
				context.put("contact", contact);
				app.appData.put("Messages.Contacts.ContactInquiry", context);
				
				//WE ALSO ADD THE CONTACT TO COMPANY CONTACTS
				updateContact(app,context,contact);
	
			}
			catch (Exception e)
			{
				throw new ContactsException("error executing addContactInquiry.", e);
			}
		}
		public void checkContactOptions(IApplication app, IRuleContext context)
		{

			try
			{
				IBizObjList l = app.appData.get("DA.Contacts.GetContactTypes", new FlexiMap()) as IBizObjList;
				if (l.numElements == 0)
				{
					IBizObjList types = getContactTypes(app, context);
					foreach (IBizObj option in types)
						app.appData.put("DA.Contacts.AddContactType", option);
				}
			}
			catch (Exception e)
			{
				throw new ContactsException("error executing checkContactOptions", e);
			}
		}

		public IBizObjList getCountries(IApplication app, IRuleContext context)
		{
			IBizObjList list = app.appData.get("DA.Contacts.GetCountries", new FlexiMap()) as IBizObjList;
			return list;
		}
		public IBizObjList getContactTypes(IApplication app, IRuleContext context)
		{
			//return app.appData["contactOptions.types"] as IBizObjList;
			IBizObjList list = app.appData.get("DA.Contacts.GetContactTypes", new FlexiMap()) as IBizObjList;
			return list;
		}
		
		public IBizObjList getContacts(IApplication app, IRuleContext context,DetailLevel detailLevel, Swaf.BGuid.Guid companyId, IBizObj filter)
		{
			try
			{
				context.put("companyId", companyId==null?ActiveCompanyId:companyId);
				context.put("listDetailLevel", detailLevel);
				context.put("criteria", filter);
				IBizObjList contacts = app.appData.get("DA.Contacts.GetCompanyContacts", context) as IBizObjList;
				return contacts;
			}
			catch (Exception e)
			{
				throw new ContactsException("error executing getContacts.", e);
			}
		}

		public IBizObj getContactDetail(IApplication app, IRuleContext context, Swaf.BGuid.Guid id)
		{
			try
			{
				context.put("contactId", id);
				IBizObj contact = App.appData.get("DA.Contacts.GetContactDetail", context) as IBizObj;
				if (contact == null)
				{
					contact = app.bizObjMgr.create("Contact");
					contact.put("id", id);
					contact.put("typeId", app.appData["contactOptions.defaultType"] as Swaf.BGuid.Guid);
					IBizObjList l = contact.get("addresses") as IBizObjList;
					IBizObj obj = l.insertNew();
					obj.put("id",new Swaf.BGuid.Guid());
					obj.put("street1", "");
				}
				if (contact.containsField("addresses")
					&& ((IBizObjList)contact.get("addresses")).numElements == 1
					&& ((IBizObjList)contact.get("addresses"))[0].get("id")==null)
				{
					((IBizObjList)contact.get("addresses")).Clear();
					IBizObj obj = ((IBizObjList)contact.get("addresses")).insertNew();
					obj.put("id", new Swaf.BGuid.Guid());
					obj.put("contactId", contact.get("id"));
				}
				return contact;
			}
			catch(Exception e)
			{
				throw new ContactsException("error executing getContactDetail bizrule.", e);
			}
		}

		public void deleteContact(IApplication app, IRuleContext context, Swaf.BGuid.Guid contactId)
		{
			context.put("id", contactId);
			app.appData.put("DA.Contacts.DeleteContactAddresses", context);
			app.appData.put("DA.Contacts.DeleteContact", context);
		}

		public void addAddress(IApplication app,IRuleContext context,IBizObj address)
		{
			try
			{
				if (address.isNew)
					app.appData.put("DA.Contacts.CreateAddress", address);
				else
					if(address.isDirty())
						app.appData.put("DA.Contacts.UpdateAddress", address);
			}
			catch (Exception e)
			{
				throw new ContactsException("error executing createAddress bizrule", e);
			}
		}
		public IBizObj updateContact(IApplication app, IRuleContext context, IBizObj contactInfo)
		{
			try
			{
				if (contactInfo == null)
					throw new ContactsException("required contact information param is null.");
				
				if (contactInfo.isNew)
				{
					if (contactInfo.get("id") == null)
						contactInfo.put("id", new Swaf.BGuid.Guid());
					if(!contactInfo.isDirty("companyId"))
						contactInfo.put("companyId", ActiveCompanyId);
					IBizObj info = App.appData.get("DA.Contacts.GetContact", contactInfo) as IBizObj;
					if(info==null)
						App.appData.put("DA.Contacts.CreateContact", contactInfo);
				}
				else
					if (contactInfo.isDirty())
						App.appData.put("DA.Contacts.UpdateContact", contactInfo);
				
				if ("true".Equals(contactInfo.get("isUser").ToString().ToLower()))
				{
					IBizObj user = app.bizObjMgr.create("User");
					user.put("id", contactInfo.get("id"));
					user.put("userName", contactInfo.get("defaultLoginName"));
					user.put("password", contactInfo.get("defaultPassword"));
					user.put("companyId", ActiveCompanyId);
					user.put("enable", 1);
					IResults rc = app.ruleMgr.execute("setupUserProfile", user);
					if (rc.ExceptionInfo != null)
						throw new ContactsException("error executing setupUserProfile in updateContact bizrule.", rc.ExceptionInfo);
				}
				foreach (IBizObj address in contactInfo.get("addresses") as IBizObjList)
				{
					address.put("contactId", contactInfo.get("id"));
					addAddress(app, context, address);
				}
				if (contactInfo == null)
					return null;
				return contactInfo;
			}
			catch (Exception e)
			{
				throw new ContactsException("error executing updateContact bizrule.", e); 
			}
		}
		
		
		public string getCompanyContactTemplate(IApplication app, IRuleContext context,IBizObj contact, Swaf.Security.Privileges priv, string parentIds, string pageName)
		{
			//string template = resultValue as string;
			//if (template != null)
			//    template += ",Contacts.Gui.AspNet.CompanyContactDisplay";
			//return template;
			return "Contacts.Gui.AspNet.CompanyContactDisplay";
		}
	}
}
